<?php
class PromotionFilterListForm extends Form
{
    function PromotionFilterListForm()
    {
        Form::Form('PromotionFilterListForm');

        CGlobal::$website_title = 'Các loại khuyến mãi';

        Db_Promotion::getPromotionFilter();

        #############################################################################
        #Xóa promotion_filter
        $rfilter_id=(int)Url::get('rfilter_id',0);

        if($rfilter_id)
        {
            DB::delete("promotion_filter",	"id=$rfilter_id");

            Db_Promotion::getPromotionFilter();

            Url::redirect_current(array('cmd'));
        }
        #Xóa promotion_filter
        #############################################################################

    }

	function on_submit()
    {
        $rfsubmit = Url::get("rfsubmit");

        if($rfsubmit)//Cập nhật thuộc tính
        {
            $rf_id = (int)implode(array_keys($rfsubmit));

            if($rf_id)
            {
                $fgroup = DB::select("promotion_filter","id=$rf_id");

                if($fgroup)
                {
                    $gf_name 	= Url::get('gf_name');
                    $gf_pos 	= Url::get('gf_pos');

                    if(isset($gf_name[$rf_id],$gf_pos[$rf_id]) && $gf_name[$rf_id] && $gf_pos[$rf_id])
                    {
                        $gf_name 	= $gf_name[$rf_id];
                        $gf_pos 	= $gf_pos[$rf_id];

                        if($fgroup['name'] != $gf_name || ($fgroup['pos'] != $gf_pos && $gf_pos > 0))//Đổi tên, vị trí
                        {
                            $max_pos	=	(int)DB::fetch("SELECT max(pos) AS max_pos FROM promotion_filter",'max_pos',0);

                            if($gf_pos > $max_pos)
                            {
                                $gf_pos = $max_pos;
                            }

                            DB::update('promotion_filter', array("name" => $gf_name, "pos" => $gf_pos) , "id=$rf_id");

                            if(DB::select('promotion_filter',"pos=$gf_pos AND id!=$rf_id"))
                            {
                                DB::query("UPDATE promotion_filter SET pos=pos+1 WHERE pos>=$gf_pos AND pos<={$fgroup['pos']} AND id!=$rf_id");
                            }

                            Db_Promotion::getPromotionFilter();
                        }
                    }
                }
            }

            Url::redirect_current(array('cmd'));
        }

        $submit = Url::get("submit");
//        var_dump($submit);die;

        if($submit == "Thêm thuộc tính")
        {
            $rfilter_name	= Url::get('rfilter_name');
            $rfilter_pos	= Url::get('rfilter_pos');

            if($rfilter_name!='' && $rfilter_pos)
            {
                $promotion_filter =	array(
                                            'name'		=>$rfilter_name,
                                            'pos'		=>$rfilter_pos
                                         );

                $max_pos	=	(int)DB::fetch("SELECT max(pos) AS max_pos FROM promotion_filter",'max_pos',0);

                if($rfilter_pos	>	$max_pos || $rfilter_pos<=0)
                {
                    $rfilter_pos	=	$max_pos+1;
                }

                $promotion_filter['pos'] = $rfilter_pos;

                $id = DB::insert('promotion_filter', $promotion_filter);

                if($id && DB::select('promotion_filter','pos="'.$rfilter_pos.'" AND id!='.$id))
                {
                    DB::query('UPDATE promotion_filter SET pos=pos+1 WHERE pos>='.$rfilter_pos.' AND id!='.$id);
                }
            }

            Db_Promotion::getPromotionFilter();

            Url::redirect_current(array('cmd'));
        }

        Url::redirect_current(array('cmd'));
    }

    function draw()
    {
        global $display;
        $this->beginForm();

        $promotion_filters = Db_Promotion::getPromotionFilter();
        $max_pos_rfilter = (int)DB::fetch("SELECT max(pos) AS max_pos FROM promotion_filter", 'max_pos', 0);
        $max_pos_rfilter += 1;
        $display->add('option_rfilter_pos',HTML::getOptionNum(1,$max_pos_rfilter,$max_pos_rfilter));

        $display->add('promotion_filters', $promotion_filters);

        $display->output('PromotionFilterList');

        $this->endForm();
    }
}

?>